<?php
ptaste();
require_once(dirname(__FILE__)."/interfaces.php");


abstract class ReplaceProcessor
{
	abstract function on_replace($node,$level,$doc,$param);
}

class JEngXReplacer implements IJXmlProducer
{
	public $doc=null;
	public $node=null;
	public $proc=null;
	public $attr=false;
	public $attr_val=false;
	public $param=null;

  function __construct($doc,$node,$proc,$param,$attr=false,$attr_val=false)
  {
    $this->doc=$doc;
	$this->node=$node;
	$this->proc=$proc;
	$this->attr=$attr;
	$this->attr_val=$attr_val;
	$this->param=$param;
  }
  
  function prepare()
  {
  }
  
  function rec_proc($node,$level)
  {
    while($node)
	{
		if ($node->nodeName==$this->node)
		{
		   $proc=true;
		   if ($this->attr!=false)
			   if ($node->attributes[$this->attr]!=$this->attr_val) $proc=false;
		   if ($proc)
		   {
		     if ($this->proc->on_replace($node,$level,$this->doc,$this->param)==false)
			   return;
		   }
		}
		if ($node->hasChildNodes())
		{
		  $this->rec_proc($node->firstChild,$level+1);
		}
		$node=$node->nextSibling;
	}
  }
  
  function render()
  {
    $this->prepare();
	$this->rec_proc($this->doc->documentElement,0);
	return $this->doc;
  }
  
  static function s_render()
  {
    $doc=func_get_arg(0);
	$node=func_get_arg(1);
	$proc=func_get_arg(2);
	$param=func_get_arg(3);
	$attr=false;
	$attr_val=false;
	if (func_num_args()>5)
	{
	 $attr=func_get_arg(4);
	 $attr=func_get_arg(5);
	}
	$obj=new JEngXReplacer($doc,$node,$proc,$param,$attr,$attr_val);
	return $obj->render();
  }
}

?>